//
//  EditingViewController.m
//  iExpense
//
//  Created by WU, Zhenyu on 7/30/09.
//  Copyright 2009 National Instruments. All rights reserved.
//

#import "EdittingViewController.h"


@implementation EdittingViewController

@synthesize editedObject;
@synthesize editedFieldKey;
@synthesize editedFieldName;
@synthesize textField;
@synthesize textView;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = editedFieldName;
	
	// Configure the save and cancel buttons.
	UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
	self.navigationItem.rightBarButtonItem = saveButton;
	[saveButton release];
	
	UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
	self.navigationItem.leftBarButtonItem = cancelButton;
	[cancelButton release];
}


- (void)viewWillAppear:(BOOL)animated {
	
	[super viewWillAppear:animated];
	
	if(isNumber) {
		textField.hidden = NO;
		textView.hidden = YES;

		NSNumber *num = [editedObject valueForKey:editedFieldKey];
		NSString *numStr = [[NSString alloc] initWithFormat:@"%1.2f",[num doubleValue]];

		textField.text = numStr;
		textField.placeholder = numStr;
		[numStr release];
		[textField becomeFirstResponder];
	} else {
		textField.hidden = YES;
		textView.hidden = NO;
		textView.text = [editedObject valueForKey:editedFieldKey];
		[textView becomeFirstResponder];
	}
	

}


#pragma mark -
#pragma mark Save and cancel operations

- (IBAction)save {
	
	// Set the action name for the undo operation.
	NSUndoManager * undoManager = [[editedObject managedObjectContext] undoManager];
	[undoManager setActionName:[NSString stringWithFormat:@"%@", editedFieldName]];
	
	if(isNumber) {
		NSNumber *num = [[NSNumber alloc] initWithDouble: [textField.text doubleValue]];
		[editedObject setValue:num forKey:editedFieldKey];
		[num release];
	} else {
		[editedObject setValue:textView.text forKey:editedFieldKey];
	}
	
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)cancel {
    // Don't pass current value to the edited object, just pop.
    [self.navigationController popViewControllerAnimated:YES];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[textField release];
	[textView release];
	[editedObject release];
	[editedFieldKey release];
	[editedFieldName release];
    [super dealloc];
}


@end
